#include <math.h>
#include <iostream>
#include "star_recognition.h"

star_recognition::star_recognition(QWidget *parent) :
	QWidget(parent), center_ra(0), center_dec(0), visibility_radius(
			SECS_IN_DEGREE * 1), stars()
{
	ui.setupUi(this);
	scene = new QGraphicsScene(this);
	scene->setSceneRect(QRect(0, 0, SECS_IN_DEGREE, SECS_IN_DEGREE));
	ui.gv->setScene(scene);
	ui.gv->scale(0.1, 0.1);
	rescene_stars();
}

star_recognition::~star_recognition()
{

}

void star_recognition::set_stars(std::vector<Star> const & stars)
{
	this->stars = stars;
	rescene_stars();
}

QColor star_recognition::cv_to_q(cv::Scalar color)
{
	return QColor(cvRound(color[2]), cvRound(color[1]), cvRound(color[0]));
}

QString star_recognition::to_2d(int a)
{
	if (a < 10)
		return QString("0") + QString::number(a);
	else
		return QString::number(a);
}

void star_recognition::renew_params()
{

	QString text = "";
	int ra_sidsecs = center_ra * 24 / 360;

	text = QString("J") + to_2d(ra_sidsecs / SECS_IN_DEGREE);
	ra_sidsecs %= SECS_IN_DEGREE;
	text += to_2d(ra_sidsecs / SECS_IN_MINUTE) + to_2d(ra_sidsecs
			% SECS_IN_MINUTE);
	int dec_secs = center_dec;
	if (center_dec < 0)
	{
		text += "-";
		dec_secs *= -1;
	}
	else
	{
		text += "+";
	}
	text += to_2d(dec_secs / SECS_IN_DEGREE);
	dec_secs %= SECS_IN_DEGREE;
	text += to_2d(dec_secs / SECS_IN_MINUTE) + to_2d(dec_secs % SECS_IN_MINUTE);
	text.append(" rad=").append(QString::number(visibility_radius, 10));
	ui.radius->setText(text);
	ui.radius->repaint();
}

void star_recognition::rescene_stars()
{
	Position center(center_ra, center_dec);
	scene->setBackgroundBrush(QBrush(QColor(0, 0, 0)));
	scene->clear();
	for (size_t i = 0; i < stars.size(); i++)
	{
		if (center.sqr_of_distance(stars[i]) <= (long long) visibility_radius
				* visibility_radius)
		{
			int radius = stars[i].get_rad(80, 1);
			scene->addEllipse(SECS_IN_DEGREE / 2
					* center.get_ra_shift(stars[i]) / visibility_radius
					+ SECS_IN_DEGREE / 2, SECS_IN_DEGREE / 2
					* center.get_dec_shift(stars[i]) / visibility_radius
					+ SECS_IN_DEGREE / 2, radius * 2, radius * 2, QPen(cv_to_q(
					stars[i].color)));
		}
	}
	ui.gv->repaint();
	renew_params();
}

void star_recognition::keyPressEvent(QKeyEvent * event)
{
	switch (event->key())
	{
	case Qt::Key_Equal:
	{
		visibility_radius += SECS_IN_DEGREE;
		if (visibility_radius > 60 * SECS_IN_DEGREE)
		{
			visibility_radius = 60 * SECS_IN_DEGREE;
		}
	}
		break;
	case Qt::Key_Minus:
	{
		visibility_radius -= SECS_IN_DEGREE;
		if (visibility_radius < SECS_IN_DEGREE)
		{
			visibility_radius = SECS_IN_DEGREE;
		}
	}
		break;
	case 65:
	{
		center_ra -= SECS_IN_DEGREE;
		if (center_ra < 0)
		{
			center_ra = SECS_IN_DEGREE * 360 + center_ra;
		}
	}
		break;
	case 87:
	{
		center_dec += SECS_IN_DEGREE;
		if (center_dec > 90 * SECS_IN_DEGREE)
		{
			center_dec = 90 * SECS_IN_DEGREE;
		}
	}
		break;
	case 68:
	{
		center_ra += SECS_IN_DEGREE;
		if (center_ra > SECS_IN_DEGREE * 360)
		{
			center_ra = center_ra - SECS_IN_DEGREE * 360;
		}
	}
		break;
	case 83:
	{
		center_dec -= SECS_IN_DEGREE;
		if (center_dec < -90 * SECS_IN_DEGREE)
		{
			center_dec = -90 * SECS_IN_DEGREE;
		}
	}
		break;
	}
	rescene_stars();
}

